<?php
/* 
 * Copyright (c) 2011 Antoine d'Otreppe de Bouvette
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

class StandardRequestFactory {
    /**
     * 
     * @return Request 
     */
    public function buildRequest() {
        $siteAddr = new WebsiteAddress();
        $siteAddr->setHost($_SERVER['HTTP_HOST']);
        $siteAddr->setPort(array_get($_SERVER, 'HTTP_PORT'));
        if (array_get($_SERVER, 'https') === 'on') {
            $siteAddr->setProtocol('https');
        }
        else {
            $siteAddr->setProtocol('http');
        }
        
        $request = new Request();
        $request->setMethod($_SERVER['REQUEST_METHOD']);
        $request->setBody($_POST);
        $request->setQuery($_GET);
        $request->setWebsiteAddress($siteAddr);
        
        // Determine website path and request URI
        // This is the tricky part, as the methods to do that depend on
        // whether the url has been rewritten or not.
        if (array_key_exists('REDIRECT_URL', $_SERVER)) {
            // Ok, this is a mod_rewrite
            $dir = dirname($_SERVER['PHP_SELF']);
            $siteAddr->setDirectory($dir);
            $siteAddr->setPath($dir);
            
            $uri = $_SERVER['REDIRECT_URL'];
            if ($dir !== '/') {
                $uri = substr($uri, strlen($dir));
            }
            $request->setUri($uri);
        }
        else {
            $siteAddr->setPath($_SERVER['SCRIPT_NAME']);
            $siteAddr->setDirectory(dirname($_SERVER['SCRIPT_NAME']));
            $request->setUri(array_get($_SERVER, 'PATH_INFO', '/'));
        }
        
        return $request;
    }
    
    
    /**
     *
     * @return WebsiteAddress
     */
    private function makeWebsiteUrl() {
        $url = new WebsiteAddress();
        
        if (isset($_SERVER['PATH_INFO'])) {
            // We have the file in the url
            $url->setPath($_SERVER['SCRIPT_NAME']);
        }
        
        $self = $_SERVER['PHP_SELF'];
        
        // Make sure it never ends with '/'
        if (substr($self, -1) == '/') {
            $self = substr($self, 0, -1);
        }
        
        $absoluteUri = $_SERVER['REQUEST_URI'];
        
        if (substr($absoluteUri, 0, strlen($self)) == $self) {
            $url->setPath($self);
        }
        else {
            $dir = dirname($self);
            if (substr($absoluteUri, 0, strlen($dir)) == $dir) {
                $url->setPath($dir);
            }
            else {
                $url->setPath('');
            }
        }
        
        return $url;
    }
}
